package com.zhiyou.servlet.contract;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zhiyou.model.Contract;
import com.zhiyou.service.ConService;
import com.zhiyou.serviceImpl.ConServiceImpl;

/**
 * Servlet implementation class ConAddServlet
 */
@WebServlet("/ConAddServlet")
public class ConAddServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ConAddServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		ConService cs = new ConServiceImpl();
		int flat_id = Integer.parseInt(request.getParameter("flat_id"));
		int tenant_id = Integer.parseInt(request.getParameter("tenant_id"));

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String contract_date = request.getParameter("lc_contract_date");
		String contract_start_date = request.getParameter("lc_contract_start_date");
		String contract_end_date = request.getParameter("lc_contract_end_date");
		
		Date lc_contract_date = null;
		Date lc_contract_start_date = null;
		Date lc_contract_end_date = null;
		try {
			lc_contract_date = sdf.parse(contract_date);
			lc_contract_start_date = sdf.parse(contract_start_date);
			lc_contract_end_date = sdf.parse(contract_end_date);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		double lc_total_rent = Double.parseDouble(request.getParameter("lc_total_rent"));
		int lc_pay_way = Integer.parseInt(request.getParameter("lc_pay_way"));
		double lc_deposit = Double.parseDouble(request.getParameter("lc_deposit"));
		int lc_period_payment = Integer.parseInt(request.getParameter("lc_period_payment"));
		String lc_contract_signer = request.getParameter("lc_contract_signer");
		int lc_contract_state = Integer.parseInt(request.getParameter("lc_contract_state"));
		Contract contract = new Contract(flat_id, tenant_id, lc_contract_date, lc_contract_start_date, lc_contract_end_date, lc_total_rent, lc_pay_way, lc_deposit, lc_period_payment, lc_contract_signer, lc_contract_state);
		boolean flag = cs.add(contract);
		if(flag){
			request.setAttribute("msg", "添加成功");
			request.getRequestDispatcher("/ConShowServlet?1=1").forward(request, response);
		}else{
			request.setAttribute("msg", "添加失败");
			request.getRequestDispatcher("/view/contract/add.jsp").forward(request, response);
		}
		
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
